import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

// 创建路由表
const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    component: () => import("../views/MyHome.vue"),
    children: [
      {
        path: "/home",
        redirect: "/home/movies",
      },
      {
        path: "/home/movies",
        component: () => import("../views/home/MyMovies.vue"),
        children: [
          {
            path: "/home/movies",
            redirect: "/home/movies/hot",
          },
          {
            path: "/home/movies/hot",
            component: () => import("../components/home/MyList.vue"),
            meta: {
              requireAlive: true,
            },
          },
          {
            path: "/home/movies/cinema",
            component: () => import("../components/home/MyCinema.vue"),
            meta: {
              requireAlive: true,
            },
          },
          {
            path: "/home/movies/wait",
            component: () => import("../components/home/MyWait.vue"),
            meta: {
              requireAlive: true,
            },
          },
          {
            path: "/home/movies/classic",
            component: () => import("../components/home/MyClassic.vue"),
          },
        ],
      },
      {
        path: "/home/video",
        component: () => import("../views/home/MyVideo.vue"),
        meta: {
          title: "猫眼视频",
        },
      },
      {
        path: "/home/mini",
        component: () => import("../views/home/MyMini.vue"),
        meta: {
          title: "猫眼小视频",
        },
      },
      {
        path: "/home/show",
        component: () => import("../views/home/MyShow.vue"),
        meta: {
          title: "猫眼演出",
        },
        // 也是前置守卫，单个路由的守卫
        beforeEnter: (to, from, next) => {
          if (localStorage.getItem("token")) {
            next();
          } else {
            next("/login");
          }
        },
      },
      {
        path: "/home/mine",
        component: () => import("../views/home/MyMine.vue"),
        // 元信息, 对象里面可以随意定义
        meta: {
          requireLogin: true,
          title: "我的猫眼",
        },
      },
    ],
  },
  {
    path: "/detail/:id",
    props: true,
    component: () => import("../views/MyDetail.vue"),
  },
  {
    path: "/city",
    component: () => import("../views/MyCity.vue"),
  },
  {
    path: "/login",
    component: () => {
      return import("../views/MyLogin.vue");
    },
  },
  {
    path: "*",
    component: () => import("../views/NotFound.vue"),
  },
];

// 创建路由实例
const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    // return { x: 0, y: 0 }

    // 当我们点击前进或者后退的时候，就会有savedPosition
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
});

// 解决重复点击的报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 全局的前置守卫
// 当任意的路由发生跳转之前都会执行
router.beforeEach((to, from, next) => {
  // console.log(to);
  // to: 去的那个页面的路由信息
  // from: 之前页面的路由信息
  // console.log(to);
  // console.log(from);
  // next(); // 守卫放行了
  // next(false); // 守卫不放行
  // next("/login"); // 重定向

  // 路由鉴权
  if (to.meta.requireLogin) {
    // 判断要去的那个页面是否是需要登录的页面
    if (localStorage.getItem("token")) {
      // 判断是否登录过了
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

// 全局解析守卫,
// 用法和全局的前置守卫完全相同, 它们的执行时机略有不同
// router.beforeResolve((to, from, next) => {})

// 全局后置钩子
router.afterEach((to) => {
  // ...
  document.title = to.meta.title || "猫眼电影 | 娱乐看我的";
});

export default router;
